/**
 * service_message js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var  tournote = require('../mongo/tournote');
var cache=require('../redis/usercache');
var message=require('../db/message');

/*
 * 获取消息列表，
 */
function processList(request,response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	message.list(request.token,function(wrap){
		if(!wrap){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
		}else{
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);	
			if(callback){
				callback(response);
			}
			
			return true;
		}
	
	});
	
}

/*
 * 修改游迹相关内容
 */
function processUpdate(request,response,callback){
	/*
	 * 修改游迹目前不实现
	 */	
}

/*
 * 删除游迹
 */
function processDelete(request,response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var  param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param._id ===null || typeof(param._id)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':_id';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		tournote.deleteTourNote(entity.userID,param._id,function (error,obj){
		    if(error !==null){
		    	response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
		    }
		    	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			if(callback){
				callback(response);
			}
		 });
	
	});

	
	
}

/*
 * 发布游迹
 */
function processPublish(request,response,callback){
	console.log('processPublish222');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
    try {
        param = JSON.parse(data);    	
	} catch (e) {
		
	}
	
	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	/*
	 *     ID:
           userID:
           title:
           coverURL:
           followedNum:
           commentedNum:
           praisedNum:
           content:[{},{}];
           createTime:
           updateTime:
           latestTime:
           items:[{}]
	 * 
	 */
	
//	TEXT:3
//	text:aaa
//	type=3;
//	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		console.log(entity);
	    tournote.insertTourNote(entity,param,function (error,obj){
	    	if(error !==null){
	    		response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
	    		
	    	}
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);	
			if(callback){
				callback(response);
			}
	    });

	});
	
}

/*
 * 获取我的游迹
 */

//MessageStatus:{
//	RUNNING:1,
//	READY:2,
//	ENDED:20,
//},
function processOfficial(request,response,callback){
	console.log('processOfficial');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	message.list(request.token,function(wrap){
		if(!wrap){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
		}else{
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);	
			if(callback){
				callback(response);
			}
			
			return true;
		}
	
	});

	
}



function processDetail(request,response,callback){
	console.log('processDetail');

	var data =request.data;
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param._id ===null || typeof(param._id)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':_id';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(request.token===null || typeof(request.token)==='undefined'){
		tournote.detailTourNote(param._id,0,function(err,obj){
			
			if(err){
				response.errorCode = err.number;
				response.message= err.message;
				if(callback){
					callback(response);
				}
				return;
			}
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);	
			if(callback){
				callback(response);
			}
		});
	}else{
		cache.getByToken(request.token,function(tk,entity){
			userID=0;
			if(entity){
				userID=entity.userID;

			}

			tournote.detailTourNote(param._id,userID,function(err,obj){
				
				if(err){
					response.errorCode = err.number;
					response.message= err.message;
					if(callback){
						callback(response);
					}
					return;
				}
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(obj);	
				if(callback){
					callback(response);
				}
			});
			
		});
	}
	
	
}

module.exports={
		processList:processList,
		processUpdate:processUpdate,
		processDelete:processDelete,
		processPublish:processPublish,
		processOfficial:processOfficial,	
		processDetail:processDetail,
	};




